<?php
namespace app\index\controller;


use app\admin\util\Response;
use app\index\model\PicCommentModel;
use app\index\model\PicImgModel;
use app\index\model\PicLikeModel;
use app\index\model\PicModel;
use app\index\util\Page;
use app\index\validate\PicValidate;
use think\Config;
use think\Db;
use think\Request;
use think\Session;

class Pic extends BaseMessage
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->_template_index = 'index';
        $this->_template_ajax_list = 'pic_list';
        $this->_template_add = 'add';
        $this->_template_detail = 'detail';
        $this->_model = new PicModel();
        $this->_like_model = new PicLikeModel();
        $this->_img_model = new PicImgModel();
        $this->_comment_model = new PicCommentModel();
        $this->_validate = new PicValidate();
    }
    protected function _before_add()
    {
        $this->assign('action',url('insert'));
        $id = input('id',0,'intval');
        $this->assign('back_url',url('index'));
        if($id != 0){
            $where = [];
            $where['id'] = $id;
            $where['uid'] = $this->_user_id;
            $vo = $this->_model->where($where)->find();
            if($vo){
                $this->assign('vo',$vo);
                $this->assign('action',url('update'));
            }
            $this->assign('back_url',url('my_list'));
        }
    }

    /**
     * 输出列表页
     * @return \think\response\View
     */
    public function index()
    {
        $order = input('order',1,'intval');
        $this->assign('order',$order);
        $data = $this->_get_list($order,$this->_size,1);
        $list = $data['data'];
        $this->assign('list_end',$data['is_end']);
        $this->assign('list',$list);
        return view($this->_template_index);
    }


    /**
     * 获取列表方式
     * @param $order
     * @param $size
     * @param $page
     * @return array
     */
    protected function _get_list($order,$size,$page){
        if($order == 1){
            $list = $this->_model->field('*')->alias('m')->join('(SELECT count(id) as like_num,message_id FROM '.$this->_like_model->getTable().' GROUP BY message_id) AS c',' m.id = c.message_id','LEFT')->where(['m.status' => 1])->order('like_num desc')->select();
        }elseif($order == 2){
            $list = $this->_model->field('count(*) as comment_num,m.*')->alias('m')->join($this->_comment_model->getTable().' c ',' m.id = c.message_id','LEFT')->where(['m.status' => 1])->order('m.update_time desc')->group('m.id')->select();
        }elseif($order == 3){
            $list = $this->_model->where(['status' => 1])->order('pv desc')->select();
        }
        $model = new PicImgModel();
        foreach ($list as &$value){
            $info = $model->where(['message_id'=>$value['id']])->find();
            $value['img'] = $info['path'];
            $value['like_num'] = $this->_get_like_num($value['id']);
            $value['comment_num'] = $this->_get_comment_num($value['id']);
        }
        return Page::return_page_data($list,$size,$page);
    }
    public function insert(){
        $param = [];
        $param['detail'] = input('detail','','htmlspecialchars');
        $param['uid'] = $this->_user_id;
        if(Config::get('site.message_default_status') == 1){
            $param['status'] = 1;
        }else{
            $param['status'] = 0;
        }
        $imgs = input('img/a','','htmlspecialchars');
        $validate = new PicValidate();
        $result = $validate->check($param);
        if($result === false){
            Response::json(-1,$validate->getError());
        }else{
            $model = new PicModel([],['after_insert'=>function(PicModel $model)use($imgs){
                $data = [];
                if(is_array($imgs)) {
                    $imgs = array_reverse($imgs);
                    foreach ($imgs as $value) {
                        $temp = [];
                        $temp['message_id'] = $model->getLastInsID();
                        $temp['path'] = $value;
                        $data[] = $temp;
                    }
                }
                $model->imgs()->saveAll($data);
            }]);
            $result = $model->isUpdate(false)->save($param);
            if($result){
                Response::json(1,'操作成功');
            }else{
                Response::json(-1,'添加失败');
            }
        }
    }
    public function my_list(){
        $page = input('page',1,'intval');
        $list = $this->_model->where(['uid'=>$this->_user_id])->select();
        foreach ($list as &$value){
            $info = $this->_img_model->where(['message_id'=>$value['id']])->find();
            $value['img'] = $info['path'];
            $value['like_num'] = $this->_get_like_num($value['id']);
            $value['comment_num'] = $this->_get_like_num($value['id']);
        }
        $result =  Page::return_page_data($list,$this->_size,$page);
        $this->assign('list',$result['data']);
        if(Request::instance()->isAjax()){
            $content = $this->fetch('ajax_my_list');
            $result['content'] = $content;
            Response::json(1,'加载成功',$result);
        }else{
            $list = $result['data'];
            $this->assign('list_end',$result['is_end']);
            return view();
        }
    }
    public function update(){
        $id = input('id',0,'intval');
        $where = [];
        $where['id'] = $id;
        $where['uid'] = $this->_user_id;
        if(!$this->_model->where($where)->find()){
            Response::json(-1,'您所更新的资料不存在');
        }
        $param = [];
        $param['detail'] = input('detail','','htmlspecialchars');
        $imgs = input('img/a','','htmlspecialchars');
        if(!is_array($imgs) || count($imgs) == 0){
            Response::json(-1,'至少需要上传1张图片');
        }
        $result = $this->_validate->check($param);
        if($result === false){
            Response::json(-1,$this->_validate->getError());
        }else{
            $this->_model->save($param,$where);
            $this->_img_model->where(['message_id'=>$id])->delete();
            $data = [];
            foreach ($imgs as $value) {
                $temp = [];
                $temp['message_id'] = $id;
                $temp['path'] = $value;
                $data[] = $temp;
            }
            $this->_img_model->saveAll($data);
            if($result){
                Response::json(1,'操作成功');
            }else{
                Response::json(-1,'添加失败');
            }
        }
    }
}
